package com.msk.web.controller.wlhy;

import com.msk.common.core.controller.BaseController;
import com.msk.common.core.domain.R;
import com.msk.common.core.domain.model.LoginUser;
import com.msk.framework.web.utils.TokenUtils;
import com.msk.wlhy.domain.HtDriverFile;
import com.msk.wlhy.domain.HtEvaluate;
import com.msk.wlhy.domain.HtTransportDetail;
import com.msk.wlhy.domain.LogisticsMsgWaybill;
import com.msk.wlhy.service.HtEvaluateService;
import com.msk.wlhy.service.HtTransportDetailService;
import com.msk.wlhy.service.LogisticsMsgWaybillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author llh
 * @date 2023/5/16
 * @description 运单评价接口
 */
@Api(tags = "运单评价接口")
@RestController
@RequestMapping("/api/wayBillEvaluation")
public class WayBillEvaluationController extends BaseController {

    @Autowired
    private HtTransportDetailService htTransportDetailService;

    @Autowired
    private HtEvaluateService htEvaluateService;

    @Autowired
    private LogisticsMsgWaybillService logisticsMsgWaybillService;

    /**
     * @author llh
     * @date 2023/5/17
     * @return com.msk.common.core.domain.R<?>
     * @description 获取运单评价列表      
     */
    @ApiOperation("获取运单评价列表")
    @PostMapping("/evaluationListData")
    public R<?> evaluationListData(@RequestBody HtTransportDetail htTransportDetail) {
        //获取当前登录司机
        HtDriverFile htDriverFile = TokenUtils.getDriverLogin();
        if (htDriverFile == null) {
            return R.appFail(400101, "用户未认证");
        }
        htTransportDetail.setDriverId(htDriverFile.getId());
        htTransportDetail.getParams().put("orderByStr", "e.evaluate_status");
        startPageJson(htTransportDetail.getParams(), "");
        List<Map<String, Object>> list = htTransportDetailService.findComplateDriverList(htTransportDetail);
        return R.appOk(getDataTable(list));
    }

    /**
     * @author llh
     * @date 2023/5/17
     * @return com.msk.common.core.domain.R<?>
     * @description 司机评价接口      
     */
    @ApiOperation("司机评价接口")
    @PostMapping("/saveEvaluate")
    public R<?> saveEvaluate(@RequestBody HtEvaluate evaluate) {
        Map map = new HashMap();
        try {
            LoginUser loginUser = TokenUtils.getMpLoginUser();
            evaluate.setEvaluateUserId(loginUser.getLoginId());
            //设置评价类型、评价状态
            evaluate.setEvaluateType(evaluate.getType());
            evaluate.setEvaluateStatus("1");
            evaluate.setEvaluateTime(new Date());
            htEvaluateService.save(evaluate);
            map.put("success", "true");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", "false");
        }
        return R.appOk(map);
    }

    /**
     * @author llh
     * @date 2023/6/16
     * @return com.msk.common.core.domain.R<?>
     * @description 货主评价列表接口      
     */
    @ApiOperation("货主评价列表接口")
    @PostMapping("/goodsEvaluationList")
    public R<?> goodsEvaluationList(@RequestBody LogisticsMsgWaybill logisticsMsgWaybill) {
        LoginUser loginUser = TokenUtils.getMpLoginUser();
        if (loginUser != null && "2".equals(loginUser.getLoginObject())) {
            logisticsMsgWaybill.setMemberId(loginUser.getLoginId());
            logisticsMsgWaybill.getParams().put("orderByStr", "e.evaluate_status");
            startPageJson(logisticsMsgWaybill.getParams(), "");
        }
        List<Map<String, Object>> list = logisticsMsgWaybillService.findTransportListByMemberId(logisticsMsgWaybill, loginUser);
        return R.appOk(getDataTable(list));
    }
}
